package org.example.Thread;

import org.example.config.core.ThreadPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class NestedThreadsExample2 {
    private static final AtomicBoolean stopAllThreads = new AtomicBoolean(false);


    private static List<Integer> list = new ArrayList<>();
    private static Integer zero = 1;
    private static Integer zero1 = 12;

    private static String getDate(){
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        Date now = new Date();

        String strDate = sdfDate.format(now);
        return strDate;

    }


    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(6);
        int finalI = 0;
        for (int i = 0; i < 12; i++) {
            finalI = i;
            int finalI1 = finalI;
            executor.submit(() -> {
                if (!stopAllThreads.get()) {
                    System.out.println("Outer thread " + Thread.currentThread().getName() + " is running");
                    // 外部线程的任务代码，连接数据库等
                    long zhuId = Thread.currentThread().getId();
                    System.out.println("----------11111111主线程id" + zhuId+"标识:"+ finalI1);

                    ExecutorService innerExecutor = Executors.newFixedThreadPool(6);
                    for (int j = 0; j < 6; j++) {
                        int finalJ = j;
                        innerExecutor.submit(() -> {
                            if (!stopAllThreads.get()) {
                                Long name = Thread.currentThread().getId();
                                System.out.println("Inner thread " + Thread.currentThread().getName() + " is running");
                                // 里面线程嵌套多线程的任务代码
                                try {
                                    if (finalI1 == 1){
                                        Thread.sleep(1000 + (finalI1 * 100000));
                                    }
                                    Thread.sleep(1000 + (finalI1 * 1000));
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                System.out.println("----------主线程id" + zhuId + "子线程id" + name+"主标识:"+ finalI1+"子标识"+ finalJ+getDate());
//                                if (Math.random() < 0.1) { // 以10%的概率随机抛出异常来模拟错误情况
//                                    stopAllThreads.set(true);
//                                    System.out.println("*** An error occured in inner thread " + Thread.currentThread().getName() + "! ***");
//                                }

                                if (finalI1 == 5) { // 以10%的概率随机抛出异常来模拟错误情况
                                    stopAllThreads.set(true);
                                    System.out.println("*** An error occured in inner thread " + Thread.currentThread().getName() + "! ***");
                                }
                                list.add(zero);
                                System.out.println("---------大小" + list.size());

                            }
                        });
                    }
                    System.out.println("----------主线程id" + zhuId);
                    innerExecutor.shutdown();
//                    try {
//                        innerExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println("test----------主线程id" + zhuId+"下子线程，全部执行完了，执行的子线程数量为:"+list.size());
                    System.out.println("当前----------主线程id" + zhuId+"执行完成，关闭连接"+zero1--);
                }
            });
        }
        System.out.println("实际执行了几次："+finalI);
        executor.shutdown();
        System.out.println("主线程等待了吗111");
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程等待了吗222");
        System.out.println("主线程都执行完了");
    }
}
